package com.antu.first.util;

import com.antu.first.dto.ImageHolder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2018/3/13.
 */
public class ImageHandle {

    private static final int IMAGEMAXCOUNT=6;

    public static void handleImage(HttpServletRequest request, List<ImageHolder> swiperImgList) throws IOException {
        //如果该请求中包含了多媒体资源，把该请求转化为MultipartHttpServletRequest
        MultipartHttpServletRequest multipartRequest= (MultipartHttpServletRequest) request;
        //取出详情图列表，并构建List<ImageHolder>列表对象，最多支持六张图片上传
        for (int i=0;i<IMAGEMAXCOUNT;i++){
            CommonsMultipartFile swiperImgFile= (CommonsMultipartFile) multipartRequest.getFile("swiperImg"+i);
            //如果对应的轮播图不为空，则把缩略图存放在swiperImg中
            if (swiperImgFile!=null){
                System.out.println("测试图片"+i);
                ImageHolder swiperImg=new ImageHolder(swiperImgFile.getOriginalFilename(),swiperImgFile.getInputStream());
                swiperImgList.add(swiperImg);
            }else {
                //否则跳出
                break;
            }
        }
    }

    public static ImageHolder handleImage(HttpServletRequest request) throws IOException {
        //如果该请求中包含了多媒体资源，把该请求转化为MultipartHttpServletRequest
        MultipartHttpServletRequest multipartRequest= (MultipartHttpServletRequest) request;
        CommonsMultipartFile swiperImgFile= (CommonsMultipartFile) multipartRequest.getFile("swiperImg");
        ImageHolder swiperImg = null;
        if (swiperImgFile!=null){
            System.out.println("测试图片");
            swiperImg=new ImageHolder(swiperImgFile.getOriginalFilename(),swiperImgFile.getInputStream());
        }
        return swiperImg;
    }

}
